package com.breezelin.freshmarket.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.app.BaseActivity;
import com.breezelin.freshmarket.app.FMApplication;
import com.breezelin.freshmarket.entities.data.UserInfo;
import com.breezelin.freshmarket.entities.network.ReqNRsp;
import com.breezelin.freshmarket.network.TaskCanceler;
import com.breezelin.freshmarket.network.asynctasks.LoginTask;
import com.breezelin.freshmarket.utils.LocalDataUtil;
import com.breezelin.freshmarket.utils.UserUtil;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

	/**
	 * 请求码
	 */
	public static final int CODE_LOGIN = 0X33;

	/**
	 * 账号输入框
	 */
	private EditText accountEdit;
	/**
	 * 密码输入框
	 */
	private EditText passwordEdit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		bindViews();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		TaskCanceler.cancelLoginRequests();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 将会接收注册页面的回传信息
		if (requestCode == RegisterActivity.CODE_LOGIN) {
			if (resultCode == RESULT_OK) {
				// 已经完成了注册，则将注册完成的信息向上一层页面进行回传，并关闭当前页面
				setResult(RESULT_OK);
				finish();
			} else {
				// 注册被取消，则保留在当前页面，并将回传结果设置为取消
				setResult(RESULT_CANCELED);
			}
		}
	}

	/**
	 * 绑定需要的视图控件
	 */
	private void bindViews() {

		accountEdit = (EditText) findViewById(R.id.edit_account);
		passwordEdit = (EditText) findViewById(R.id.edit_password);
		findViewById(R.id.btn_login).setOnClickListener(this);
		findViewById(R.id.btn_register).setOnClickListener(this);
	}

	/**
	 * 登录
	 */
	private void login() {
		// 从输入框中取出数据
		String account = accountEdit.getText().toString();
		final String password = passwordEdit.getText().toString();
		// 进行请求
		new LoginTask(account, password) {
			@Override
			protected void onSuccess(ReqNRsp<UserInfo> userInfoReqNRsp) {
				showToast(R.string.toast_login_success);
				// 处理用户信息
				UserInfo userInfo = userInfoReqNRsp.getBody();
				UserUtil.saveUserData(userInfo);
				// 保存密码
				UserUtil.savePassword(password);
				// 保存登录状态
				FMApplication.isLogin = true;
				// 保存token
				LocalDataUtil.getInstance().put(
						LocalDataUtil.KEY_SVR_TOKEN,
						userInfo.getToken()
				);
				// 设置结果
				setResult(RESULT_OK);
				finish();
			}

			@Override
			protected void onError() {
				super.onError();
			}
		}.execute();
	}

	/**
	 * 注册
	 */
	private void register() {
		startActivityForResult(new Intent(this, RegisterActivity.class), RegisterActivity.CODE_LOGIN);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			// 登录按钮
			case R.id.btn_login:
				login();
				break;
			// 注册按钮
			case R.id.btn_register:
				register();
				break;
		}
	}
}
